package hutool.excel;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 2:21
 * @since JDK 1.8
 */
public class ExcelRead {

    public static void main(String[] args) {
        List<Map<String, Object>> mapList=getExcelContent2(new File("C:\\Users\\xiong\\Desktop\\镇街采集点二维码数据 - 副本.xls"),"镇街采集点二维码数据.xls");
        for (int i = 0; i <mapList.size() ; i++) {
            Map<String, Object> map=mapList.get(i);
            if(map.containsKey("6")){
                System.out.println(map.get("6"));
            }
        }
//        for (int i = 0; i < mapList.size(); i++) {
//            if(i>=2){
//                Map<String, Object> objectMap=mapList.get(i);
//                String url=(String) objectMap.get("url");
//                System.out.println(i+" "+url);
//                String address=(String)objectMap.get("address");
//                //createQrCodeN(url,200,200,i+"."+address);
//            }
//        }
//         System.out.println(string);
//       mapList.forEach(e->{
//           String url=(String) e.get("url");
//           String  address=(String)e.get("address");
//           System.out.println(url+"url");
//           if(url!=null&&!"null".equals(url)){
//               createQrCodeN(url,200,200,address);
//           }
//           //System.out.println(address+" "+url);
//       });
    }



//    public static String erCodeCreate(String url,String filename){
//
//    }

//    public static void main(String[] args) {
//        createQrCodeN("",200,200,"");
//    }

    public static void createQrCodeN(String content, int width, int height,String fileName){
        QrConfig config = new QrConfig(width, height);
        //附带logo
        // 设置边距，既二维码和背景之间的边距
        config.setMargin(3);
        // 高纠错级别
        config.setErrorCorrection(ErrorCorrectionLevel.H);
        // 设置前景色，既二维码颜色（青色）
        config.setForeColor(new Color(0,60,130).getRGB());
// 设置背景色（灰色）
        config.setBackColor(new Color(242,242,242).getRGB());
       /* try {
            OutputStream  out = new FileOutputStream("");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }*/

        QrCodeUtil.generate(content, config, FileUtil.file("C:\\Users\\xiong\\Desktop\\ercode\\"+fileName+".jpg"));

    }


    public static  List<Map<String,Object>> getExcelContent2(File file, String fileName){
        List<Map<String,Object>> mapList=new ArrayList<>();
        InputStream fis = null;
        try {
            fis = new FileInputStream(file);
            Workbook workbook = null;
            if (fileName.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(fis);
            } else if (fileName.endsWith(".xls") || fileName.endsWith(".et")) {
                workbook = new HSSFWorkbook(fis);
            }
            fis.close();
            /* 读EXCEL文字内容 */
            // 获取第一个sheet表，也可使用sheet表名获取
            Sheet sheet = workbook.getSheetAt(0);
            // 获取行
            Iterator<Row> rows = sheet.rowIterator();
            Row row;
            Cell cell;
            while (rows.hasNext()) {
                row = rows.next();
                // 获取单元格
                Iterator<Cell> cells = row.cellIterator();
                int num=0;
                StringBuilder stringBuilder=new StringBuilder();
                Map<String, Object> stringObjectMap=new HashMap<>();
                while (cells.hasNext()) {
                    num++;
                    cell = cells.next();
                    String cellValue = POIUtil.getCellValue(cell);
                    stringObjectMap.put(String.valueOf(num),cellValue);
                }
                mapList.add(stringObjectMap);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }  finally {
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return mapList;
    }

    public static String getExcelContent(File file, String fileName){
        StringBuffer stringBuffer = new StringBuffer();
        InputStream fis = null;
        try {
            fis = new FileInputStream(file);
            Workbook workbook = null;
            if (fileName.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(fis);
            } else if (fileName.endsWith(".xls") || fileName.endsWith(".et")) {
                workbook = new HSSFWorkbook(fis);
            }
            fis.close();
            /* 读EXCEL文字内容 */
            // 获取第一个sheet表，也可使用sheet表名获取
            Sheet sheet = workbook.getSheetAt(0);
            // 获取行
            Iterator<Row> rows = sheet.rowIterator();
            Row row;
            Cell cell;

            while (rows.hasNext()) {
                int num=0;
                row = rows.next();
                // 获取单元格
                Iterator<Cell> cells = row.cellIterator();
                while (cells.hasNext()) {
                    cell = cells.next();
                    String cellValue = POIUtil.getCellValue(cell);
                    stringBuffer.append(cellValue+"\n");
                    num++;
                    System.out.println(num+"num"+cellValue);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }  finally {
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return stringBuffer.toString();
    }




}
